package AtomicDemo.AtomicLongDemo;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @Author: ylc
 * @Description:使用AtomicLong定义一个计数器（单例模型）
 * @Date Created in 2022-05-14 15:32
 */
public class Indicator {
    private Indicator(){

    }
    private static final Indicator INSTANCE = new Indicator();
    //提供一个公共静态方法返回该类唯一实例
    public static Indicator getInstance(){
        return INSTANCE;
    }
    //使用原子变量计数
    private final AtomicLong requestCount = new AtomicLong(0);
    private final AtomicLong successCount = new AtomicLong(0);
    private final AtomicLong failureCount = new AtomicLong(0);

    /**
     * 有新的请求的时候
     */
    public void newRequestReceive(){
        requestCount.incrementAndGet(); //总数增长
    }
    /**
     * 处理成功的时候
     */
    public void requestSuccess(){
        successCount.incrementAndGet(); //成功数+1
    }
    /**
     * 处理失败的时候
     */
    public void requestFialure(){
        failureCount.incrementAndGet(); //失败数+1
    }

    /**
     * 查看总数，成功数，失败数
     */
    public long getRequestCount(){
        return requestCount.get();
    }
    public long getRequestSuccess(){
        return successCount.get();
    }
    public long getRequestFialure(){
        return failureCount.get();
    }
}